﻿using MySimpleMvc.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MySimpleMvc.Handler
{
    /// <summary>
    /// 模拟MVC程序的单一入口
    /// </summary>
    public class Index : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            var controllerName = context.Request.QueryString["controller"];
            
            IController controller = null;
            if (string.IsNullOrEmpty(controllerName))
            {
                controllerName = "home";
            }
            switch (controllerName.ToLower())
            {
                case "home":
                    controller = new HomeController();
                    break;
                case "product":
                    controller = new ProductController();
                    break;
                default:
                    controller = new HomeController();
                    break;
            }

            var requestContext = new HttpContextWrapper
            {
                Context = context
            };
            controller.Execute(requestContext);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}